package com.learning.jquizenglish.screen.learningfeed;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.learning.jquizenglish.JQuizAppConfig;
import com.learning.jquizenglish.R;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.widget.ViewHolder;

public class LearningFeedExpandAdapter extends BaseExpandableListAdapter{

	private String[] mAnswerTitle = {"A","B","C","D","E","F"};
	private Activity mContext;
	private ArrayList<QuestionModel> mListQuestionModels;
	private LearningFeedScreen mFrag;
	
	public LearningFeedExpandAdapter(Activity context,ArrayList<QuestionModel> listQuestion){
		mContext = context;
		mListQuestionModels = listQuestion;
	}
	
	public LearningFeedExpandAdapter(LearningFeedScreen frag,Activity context,ArrayList<QuestionModel> listQuestion){
		mFrag = frag;
		mContext = context;
		mListQuestionModels = listQuestion;
	}
	
	@Override
    public Object getChild(int groupPosition, int childPosititon) {
		if (mListQuestionModels!=null) {
			return mListQuestionModels.get(groupPosition);
		}
		return null;
    }

	@Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0L;
    }

	@Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.learning_feed_question_item, null);
        }
		final QuestionModel questionModel = (QuestionModel) getChild(groupPosition, childPosition);
		TextView txtQuestionTitle = ViewHolder.get(convertView, R.id.txtQuestionTitle);
		TextView txtQuestionContent =  ViewHolder.get(convertView,R.id.txtQuestion);
		TextView txtPlayAudio		= ViewHolder.get(convertView, R.id.txtPlayAudio);
		final ListView listAnswer		=  ViewHolder.get(convertView,R.id.lvChoice);
		TextView txtComment = ViewHolder.get(convertView, R.id.txtComment);
		TextView txtHelp = ViewHolder.get(convertView, R.id.txtHelp);
		TextView txtMore = ViewHolder.get(convertView, R.id.txtMore);
		txtComment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if (mFrag!=null) {
					mFrag.onComment(questionModel);
				}
			}
		});
		txtHelp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if (mFrag!=null) {
					mFrag.onHelp(questionModel);
				}
			}
		});
		txtMore.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mFrag!=null) {
					mFrag.onMore(groupPosition);
				}
			}
		});
		if (JQuizAppConfig.IS_LISTENING_APP) {
			txtPlayAudio.setVisibility(View.VISIBLE);
			txtPlayAudio.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (mFrag!=null) {
						mFrag.onShowDialogPlayAudio(questionModel);
					}
				}
			});
		}
		
		switch (Integer.valueOf(questionModel.level_id)) {
		case 1:
			if (JQuizAppConfig.IS_LISTENING_APP) {
				txtQuestionTitle.setText("Question in \"Short Conversation\":");
			}else{
				txtQuestionTitle.setText("Question in \"English Grammar:Elementary:\"");
			}
			break;
		case 2:
			if (JQuizAppConfig.IS_LISTENING_APP) {
				txtQuestionTitle.setText("Question in \"Longer Conversation\":");
			}else{
				txtQuestionTitle.setText("Question in \"English Grammar:Intermediate:\"");
			}
			break;
		case 3:
			if (JQuizAppConfig.IS_LISTENING_APP) {
				txtQuestionTitle.setText("Question in \"Mini Talks & Lectures\":");
			}else{
				txtQuestionTitle.setText("Question in \"English Grammar:Advanced:\"");
			}
			break;
		default:
			if (JQuizAppConfig.IS_LISTENING_APP) {
				txtQuestionTitle.setText("Question in \"Short Conversation\":");
			}else{
				txtQuestionTitle.setText("Question in \"English Grammar:Elementary:\"");
			}
			break;
		}
		txtQuestionContent.setText(questionModel.title);
		final LearningFeedChildAdapter adapter = new LearningFeedChildAdapter(mContext, questionModel.answers);
		boolean isAnswered = questionModel.isAnswered;
		adapter.isAnswered = isAnswered;
		listAnswer.setAdapter(adapter);
		listAnswer.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				questionModel.isAnswered = true;
				questionModel.answerId = questionModel.answers.get(pos).id;
				adapter.chooseAnswer(pos);
				listAnswer.setEnabled(false);
			}
		});
		if(isAnswered){
			listAnswer.setEnabled(false);
		}else{
			listAnswer.setEnabled(true);
		}
		listAnswer.getLayoutParams().height =  
				mContext.getResources().getDimensionPixelSize(R.dimen.learning_feed_answer_item_height)*questionModel.answers.size()
				+mContext.getResources().getDimensionPixelSize(R.dimen.learning_feed_answer_list_margin);
		return convertView;
	}

	@Override
    public int getChildrenCount(int groupPosition) {
		return 1;
    }

	@Override
	public Object getGroup(int groupPosition) {
		if (mListQuestionModels!=null) {
			return mListQuestionModels.get(groupPosition);
		}
		return null;
	}

	@Override
	public int getGroupCount() {
		if(mListQuestionModels!=null){
			return mListQuestionModels.size();
		}
		return 0;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.learning_feed_group, parent,false);
        }
		ImageView imgIcon = ViewHolder.get(convertView, R.id.imgIcon);
		TextView tvTitle = ViewHolder.get(convertView, R.id.tvTitle);
		QuestionModel model = (QuestionModel) getGroup(groupPosition);
		if (model.isHide == true) {
			convertView.setVisibility(View.GONE);
		}else{
			convertView.setVisibility(View.VISIBLE);
		}
		if (model.isWaitingHide) {
			imgIcon.setImageResource(R.drawable.alien);
			tvTitle.setText("Why don't you want to see it");
			tvTitle.setTypeface(null, Typeface.ITALIC);
			mFrag.mListView.collapseGroup(groupPosition);
		}else{
			imgIcon.setImageResource(R.drawable.question_random);
			tvTitle.setText("Random question");
			tvTitle.setTypeface(null, Typeface.NORMAL);
			mFrag.mListView.expandGroup(groupPosition);
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	public void dontShowGroup(int groupPosition){
		mListQuestionModels.get(groupPosition).isWaitingHide = true;
		notifyDataSetChanged();
	}
	
	public void hideGroup(int groupPosition){
//		mListQuestionModels.get(groupPosition).isHide = true;
//		notifyDataSetChanged();
		mListQuestionModels.remove(groupPosition);
		notifyDataSetChanged();
	}
}
